<?php

include "connection.php";
include 'warrantypdf.php';
ob_start();
//Select the values from the database
$query = "Select * from warranties";
$result = mysql_query($query);
$count = 0;
$count2 = 0;

while ($row = mysql_fetch_array($result)){
	if ($row['email'] != ""){
	//create warranty certificate
	createPdf($row['storename'], $row['salesperson'], $row['brand'], $row['model'], $row['serialnumber'], $row['usage'], $row['purchasedate'], $row['invoicenumber'], $row['purchaseprice'], $row['firstname'], $row['lastname'], $row['contractortype'], $row['contractornumber'], $row['businessname'], $row['address'], $row['suburb'], $row['postcode'], $row['state'], $row['phone'], $row['email']);
	//set filename
	$filename = "";
	$filename = 'warranty_'.$row['serialnumber'].'.pdf';
	//send email
	//Create line break
	if (strtoupper(substr(PHP_OS,0,3)=='WIN')) { 
    $eol="\r\n"; 
	} elseif (strtoupper(substr(PHP_OS,0,3)=='MAC')) { 
    $eol="\r"; 
	} else { 
    $eol="\n"; 
	} 
	# File for Attachment 
	unset($f_name);
	$f_name=$filename;    // use relative path OR ELSE big headaches. $letter is my file for attaching. 
	unset($handle);
	$handle=fopen($f_name, 'rb'); 
	unset($f_contents);
	$f_contents=fread($handle, filesize($f_name)); 
	$f_contents=chunk_split(base64_encode($f_contents));  //Encode The Data For Transition using base64_encode(); 
	unset($f_type);
	$f_type=filetype($f_name); 
	fclose($handle); 

	# To Email Address 
	$emailaddress=$row['email']; 
	# Message Subject 
	$emailsubject="Allpower Warranty Certificate"; 
	# Message Body 
	$body="Please find attached a copy of your warranty certificate"; 
	# Common Headers 
	unset($headers);
	$headers .= 'From: info@allpower.com.au'.$eol; 
	# Boundry for marking the split & Multitype Headers 
	$mime_boundary=md5(time()); 
	$headers .= 'MIME-Version: 1.0'.$eol; 
	$headers .= "Content-Type: multipart/related; boundary=\"".$mime_boundary."\"".$eol.$eol; 
	//$msg = "";
	$headers .= "Content-Transfer-Encoding: 7bit".$eol;
	$headers .= "This is a MIME encoded message.".$eol.$eol; 
	# HTML Version 
	$headers .= "--".$mime_boundary.$eol; 
	$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol; 
	$headers .= "Content-Transfer-Encoding: 8bit".$eol.$eol; 
	$headers .= "You are receiving this message because you have recently registered a product warranty on the Allpower website.<br /><br />Find attached a copy of your warranty certificate.<br /><br />If you have any queries, you can get in touch with Allpower by emailing info@allpower.com.au.".$eol.$eol; 
	# Attachment 
	$headers .= "--".$mime_boundary.$eol; 
	$headers .= "Content-Type: application/pdf; name=\"".$filename."\"".$eol;   // sometimes i have to send MS Word, use 'msword' instead of 'pdf'
	$headers .= "Content-Transfer-Encoding: base64".$eol; 
	$headers .= "Content-Disposition: attachment;".$eol.$eol; // !! This line needs TWO end of lines !! IMPORTANT !! 
	$headers .= $f_contents.$eol.$eol;  
	# Finished 
	$headers .= "--".$mime_boundary."--".$eol.$eol;   // finish with two eol's for better security. see Injection. 
	# SEND THE EMAIL  
	mail($emailaddress, $emailsubject, "", $headers, "-f info@allpower.com.au")  or die ("There was an error sending the second mail");
	//output work done
	echo "Email sent to ".$row['firstname']. " ".$row['lastname']. ". The filename is ".$filename. ". The f_name is ".$f_name;
	echo "<br>";
	$count = $count + 1;
	set_time_limit(200);}
	else {
	//display no email address
	echo $row['firstname']. " " .$row['lastname']. " does not have an email address listed. No email sent.";
	echo "<br>";
	$count2 = $count2 + 1;
	} 
}
echo "Email sent to ".$count." people.";
echo "<br>";
echo $count2." people did not have email addresses listed. No emails sent.";
echo "<br>";
echo "Script Over Now";

$page = ob_get_contents();
ob_end_flush();
$fp = fopen("output.html","w");
fwrite($fp,$page);
fclose($fp);
?>